// pages/verify/verify.js
const app = getApp();
const config = app.globalData.config;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    code: null,
    focus: true,
    prize: null,
    userInfo: null,
    msg: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var userInfo = wx.getStorageSync('sUserInfo');
    if (!userInfo) {
      // wx.navigateTo({
      //   url: '/pages/me/me',
      // })
      wx.showToast({
        title: '请授权',
        icon: 'none'
      })
    }
    this.setData({
      userInfo: userInfo
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  onChangeCode(e) {
    this.setData({
      code: e.detail
    })
  },
  //查询兑奖码
  onQueryCode() {
    var that = this;
    if (!that.data.code) {
      wx.showToast({
        title: '兑换码必填',
        icon: 'none'
      })
      that.setData({
        focus: true,
        msg: null
      })
      return;
    }
    wx.showLoading({
      title: '查询中',
      mask: true
    })
    wx.request({
      url: config.verifyUrl,
      method: "GET",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        userId: that.data.userInfo.id,
        code: that.data.code
      },
      success: function(resp) {
        if (resp.data.result == 1) {
          that.setData({
            prize: resp.data.data,
            msg: resp.data.msg
          })
        }
      },
      fail: function(e) {},
      complete: function() {
        wx.hideLoading();
      }
    })
  },
  //奖品核销
  onExchange() {
    var that = this;
    wx.request({
      url: config.exchangeUrl,
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        userId: that.data.userInfo.id,
        code: that.data.code
      },
      success: function(resp) {
        that.setData({
          isExchange: resp.data.data,
          msg: resp.data.msg
        })
      },
      fail: function(e) {}
    })
  }
})